<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.6.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="消息中间件1,RocketMqRocketMq的优点(应用场景)1. 解耦 2. 异步 3. 削峰">
<meta property="og:type" content="article">
<meta property="og:title" content="中间件">
<meta property="og:url" content="http://example.com/2021/07/24/rocketMq/index.html">
<meta property="og:site_name" content="风轻云淡">
<meta property="og:description" content="消息中间件1,RocketMqRocketMq的优点(应用场景)1. 解耦 2. 异步 3. 削峰">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-07-24T14:51:50.000Z">
<meta property="article:modified_time" content="2022-01-20T14:23:49.497Z">
<meta property="article:author" content="WQQ">
<meta property="article:tag" content="风">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/2021/07/24/rocketMq/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://example.com/2021/07/24/rocketMq/","path":"2021/07/24/rocketMq/","title":"中间件"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>中间件 | 风轻云淡</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
	<!-- 页面点击小红心 -->
	<script type="text/javascript" src="/js/src/clicklove.js"></script>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">风轻云淡</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">清风吹拂，白云淡淡</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-首页"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-关于"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li>
        <li class="menu-item menu-item-标签"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-分类"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6"><span class="nav-number">1.</span> <span class="nav-text">消息中间件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-RocketMq"><span class="nav-number">1.0.1.</span> <span class="nav-text">1,RocketMq</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#RocketMq%E7%9A%84%E4%BC%98%E7%82%B9-%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="nav-number">1.0.1.1.</span> <span class="nav-text">RocketMq的优点(应用场景)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#RocketMq-%E5%AE%89%E8%A3%85%E6%AD%A5%E9%AA%A4"><span class="nav-number">1.0.1.2.</span> <span class="nav-text">RocketMq 安装步骤</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#RocketMq%E5%90%8D%E8%AF%8D%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.0.1.3.</span> <span class="nav-text">RocketMq名词介绍</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#RocketMq%E5%90%AF%E5%8A%A8-%E5%85%B3%E9%97%AD"><span class="nav-number">1.0.1.4.</span> <span class="nav-text">RocketMq启动&#x2F;关闭</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#RocketMQ%E7%9B%B8%E5%85%B3%E9%9D%A2%E8%AF%95%E9%A2%98"><span class="nav-number">1.1.</span> <span class="nav-text">RocketMQ相关面试题</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="WQQ"
      src="/images/aaa.jpg">
  <p class="site-author-name" itemprop="name">WQQ</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">14</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://gitee.com/wqqszz" title="GitHub → https:&#x2F;&#x2F;gitee.com&#x2F;wqqszz" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="QQ:1640611853@qq.com" title="E-Mail → QQ:1640611853@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



          </div>
        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/07/24/rocketMq/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/aaa.jpg">
      <meta itemprop="name" content="WQQ">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="风轻云淡">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          中间件
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-24 22:51:50" itemprop="dateCreated datePublished" datetime="2021-07-24T22:51:50+08:00">2021-07-24</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-01-20 22:23:49" itemprop="dateModified" datetime="2022-01-20T22:23:49+08:00">2022-01-20</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/RocketMQ/" itemprop="url" rel="index"><span itemprop="name">RocketMQ</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="消息中间件"><a href="#消息中间件" class="headerlink" title="消息中间件"></a>消息中间件</h1><h3 id="1-RocketMq"><a href="#1-RocketMq" class="headerlink" title="1,RocketMq"></a>1,RocketMq</h3><h4 id="RocketMq的优点-应用场景"><a href="#RocketMq的优点-应用场景" class="headerlink" title="RocketMq的优点(应用场景)"></a>RocketMq的优点(应用场景)</h4><pre><code>1. 解耦
2. 异步
3. 削峰
</code></pre>
<span id="more"></span>

<h4 id="RocketMq-安装步骤"><a href="#RocketMq-安装步骤" class="headerlink" title="RocketMq 安装步骤"></a>RocketMq 安装步骤</h4><pre><code>1.    [官网下载地址](http://rocketmq.apache.org/)
2.    Linux系统为例，cd /usr/local/, 解压: tar -xvf alibaba-rocketmq-3.2.6.tar.gz
3.    重命名 mv alibaba-rocketmq-3.2.6  rocketmq 
4.    cd /rocketmq
5.    cd /bin 核心文件在bin文件下
6.    bin 目录下文件没有执行权限，需要先授权，chmod +x *
7.    启动nameserver用  ./mqnamesrv 命令
8.    如果启动出现 There is insufficiont memory for java Runtime Enviroment to continue。。。。。。。
    此时启动内存不足，修改配置文件  vim runserver.sh ,默认启动内容4g,把启动内存改小会启动成功。
9.    重启 nameserver 启动成
10.    启动 broker ,修改启动内存，默认4g, vim runbroker.sh 文件
</code></pre>
<h4 id="RocketMq名词介绍"><a href="#RocketMq名词介绍" class="headerlink" title="RocketMq名词介绍"></a>RocketMq名词介绍</h4><pre><code>1. NameServer: 管理Broker; 举例:各个邮局的管理机构
2. Broker:    暂存和传输消息; 举例：邮局
3. Producer: 消息分发送者; 举例：发信人
4. Consumer: 消息消费者;    举例：收信人
5. Topic: 分区消息的种类; 一个发送者可以发一个或多个topic类型的消息；
一个消息接收者可以消费一个或多个topic类型消息
</code></pre>
<h4 id="RocketMq启动-关闭"><a href="#RocketMq启动-关闭" class="headerlink" title="RocketMq启动/关闭"></a>RocketMq启动/关闭</h4><pre><code>启动 sh bin/mqshutdown namesrv 
    
关闭 sh bin/mqshutdown namesrv 
     sh bin/mqshutdown broker
</code></pre>
<p>​    </p>
<h2 id="RocketMQ相关面试题"><a href="#RocketMQ相关面试题" class="headerlink" title="RocketMQ相关面试题"></a><strong>RocketMQ相关面试题</strong></h2><ol>
<li><p>在你所在的项目中消息队列应用场景？应用的好处是什么？弊端是什么？</p>
<p>答：多个服务之间进行数据的传输。                                                                                                                             <strong>优点：解耦，削峰，异步。</strong></p>
<p><strong>解耦</strong>：如：A服务提供的数据B,C,D服务都需要，A服务作为生产者生产消息，B,C,D服务如果都需要A服务产生的消息则直接可以去消费消息队列，不需要把A服务接口整合到B,C,D服务 ；如果后期不需要了，则可以不去消费消息，降低耦合，达到解耦的目的。</p>
<p><strong>削峰</strong>：如：在某一时间段内业务高峰期，如果所有请求直接请求A服务，可能会导致A服务因为短时间内并发过大导致系统崩溃，利用MQ可以先把请求缓存到消息队列中，进行逐步请求A服务，避免短时间内请求量过大。</p>
<p><strong>异步</strong>：一些非必要的业务逻辑以同步的方式运行会太消耗时间，可以先将请求结果返回给客户端，再去执行放到mq的消息。</p>
<p><strong>缺点：系统可用性降低，系统复杂度提高，一致性问题</strong>。</p>
<p><strong>系统可用性降低</strong>：系统引入的外部依赖越多，越容易挂掉，万一MQ挂掉了，则整个系统都会崩溃。</p>
<p><strong>系统复杂度提高</strong>：引入MQ后要考虑消息的重复消费问题，处理消息丢失的问题，消息传递的顺序性问题，都是需要进行解决的问题。</p>
<p><strong>一致性问题</strong>：A服务直接返回成功给客户端，但是B,C，D服务可能存在消息消费失败的情况，导致数据不一致，需要进行解决改问题。</p>
</li>
<li><p>KafKa ,ActiveMQ,RocketMq,RabbitMq 之间有什么区别？</p>
<table>
<thead>
<tr>
<th align="left">特性</th>
<th>ActiveMQ</th>
<th>RocketMQ</th>
<th>RabbitMQ</th>
<th>kafka</th>
</tr>
</thead>
<tbody><tr>
<td align="left">开发语言</td>
<td>java</td>
<td>java</td>
<td>erlang</td>
<td>scala</td>
</tr>
<tr>
<td align="left">单机吞吐量</td>
<td>万级</td>
<td>10万级</td>
<td>万级</td>
<td>10万级</td>
</tr>
<tr>
<td align="left">时效性</td>
<td>ms级</td>
<td>ms级</td>
<td>us级</td>
<td>ms级</td>
</tr>
<tr>
<td align="left">可用性</td>
<td>高（主从级）</td>
<td>非常高（分布式架构）</td>
<td>高（主从级）</td>
<td>非常高（分布式架构）</td>
</tr>
<tr>
<td align="left">功能特性</td>
<td>成熟的产品，在很多公司得到应用；有较多的文档；各种协议支持较好</td>
<td>MQ功能比较完善，扩展性强</td>
<td></td>
<td>只支持主要的MQ功能，                                                    像一些消息查询，                                                             消息回溯等功能没有提供，                                                      毕竟是为大数据准备的，在大数据领域应用广。</td>
</tr>
</tbody></table>
<p>综上：环境为分布式并且数据量大的业务可以选型RocketMq或者Kafka比较合适 , </p>
<p>​        数据量小的业务对吞吐量小的可以选择ActiveMQ或者RabbitMQ。</p>
</li>
<li><p>如何保证消息队列的高可用？</p>
</li>
<li><p>如何保证消息不被重复消费？(如何保证消息队列的幂等性?)</p>
<p><strong>重复消费场景一:MQ中断导致重复消费，</strong></p>
<p>如何保证不被重复消费（幂等）。</p>
<p><strong>幂等：</strong>就是指一个数据，或者一个请求，给你重复来多次，得确保对应的数据不会改变，不能出错。</p>
<p>生产者可以在MQ消息里面加一个全局唯一id，类似于订单id, 然后得到这个消息后，先根据id去查询之前有没有处理过，如果没有则进行正常处理，如果发现已经处理过了，则就不需要处理了，保证别处理重复处理的消息即可。</p>
</li>
<li><p>如何保证消息的可靠性传输？</p>
<p>1.减小丢失消息的可能性尽量采用同步的发送方式，同步等待发送结果，利用<strong>同步发送+重试机制+多个master节点</strong>，尽可能减小消息丢失的可能性。</p>
<p>2.我们在调用producer.send方法时，不指定回调方法，则默认采用同步发送消息的方式，这也是丢失几率最小的一种发送方式。</p>
<p>3.broker提供多master模式，即使某台broker宕机了，保证消息可以投递到另外一台正常的broker上。</p>
<p>​    broker提供三种模式：单master ,多master , 多master多slave</p>
<p>4,<strong>保证消息不丢失：</strong></p>
<p>​    <strong>提供主从模式，同时主从支持同步双写。</strong></p>
<p>​    即使broker设置了同步刷盘，如果broker磁盘坏了，也会导致消息丢失。因此可以给broker设置slave, 同时设置    master为 SYNC_MASTER ,然后将slave设置为同步刷盘策略。</p>
<p>此模式下，producer每发送一条消息，都会等到消息投递到master和slave都落盘成功后，broker才会当做消息投递成功，保证消息不丢失。</p>
</li>
<li><p>如何保证消息的顺序性？</p>
</li>
<li><p>如何解决消息的延时以及失效过期？</p>
</li>
<li><p>MQ消息积压大量,怎么解决？</p>
<p>如果topic下的MessageQueue 配置的足够多的话，每个消费者（Consumer）实际上会分配多个MessageQueue来进行消费，这个时候可以通过增加消费者的服务节点数量来加快消息的消费。</p>
<p>如果原来的topic下的MessageQueue 配置的不足够多，则新建一个topic，然后配置足够多的MessageQueue，然后把原有所有消费者节点的目标下的topic转向新的topic下。</p>
</li>
</ol>

    </div>

    
    
    
	
	<div>
		
			<div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
		
	</div>

    <footer class="post-footer">

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/07/24/Java/" rel="prev" title="Java基础">
                  <i class="fa fa-chevron-left"></i> Java基础
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/07/24/%E6%88%91%E7%9A%84%E7%AC%AC%E4%B8%89%E4%B8%AA%E5%8D%9A%E5%AE%A2/" rel="next" title="我的第三个博客">
                  我的第三个博客 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">WQQ</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
